package Zoo.Impl;

import Zoo.Entity.LeaderEntity;
import Zoo.Entity.ZooparkEntity;
import Zoo.Service.LeaderService;
import Zoo.Util.TableDTO;
import lesson05.utils.JdbcUtils_DBCP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

/**
 * 1、园长的登录功能
 * 2、园长所看到的园区信息界面
 * 3、园长增删改查园区功能
 */

public class LeaderServiceImpl implements LeaderService {
    @Override
    public boolean validateLeader(LeaderEntity leaderEntity) {
        String leaderName=leaderEntity.getLeaderName();
        String idNumber= leaderEntity.getIdNumeber();
        String sql ="SELECT `id_number` FROM `leaders` WHERE `name` =?";
        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            //获取数据库连接
            conn = JdbcUtils_DBCP.getConnection();
            if(conn==null){
                //如果没获取到数据库连接，则返回false
                return false;
            }
            st= conn.prepareStatement(sql);
            st.setString(1, leaderEntity.getLeaderName());
            rs = st.executeQuery();
            while(rs.next()){
                String pwd = rs.getString(1);
                if(leaderEntity.getIdNumeber().equals(pwd)){
                    //如果获取的身份证号是相对应的，则返回true
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils_DBCP.release(conn,st,rs);
        }



        //如果获取的用户名和身份证号是不匹配的，则返回false
        return false;
    }

    @Override
    public TableDTO retrieveLeader(LeaderEntity leaderEntity) {
        StringBuilder sql=new StringBuilder();
        sql.append("SELECT * FROM `zooparks` ");
        if(leaderEntity.getSearchKey()!=null && !"".equals(leaderEntity.getSearchKey().trim())){
            sql.append("where `kind` like '%"+leaderEntity.getSearchKey().trim()+"%'");
        }

        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        //查出来的数据存在这里
        TableDTO returnDTO=new TableDTO();


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            rs = st.executeQuery();
            //存放连接数据库查出来的数据
            returnDTO.setData(fillData(rs));;


            return returnDTO;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,rs);
        }
        return null;
    }
    //从数据库查出来的数据
    private Vector<Vector<Object>> fillData(ResultSet rs) throws SQLException {
        Vector<Vector<Object>> data=new Vector<>();
        while(rs.next()){
            Vector<Object> oneRecord =new Vector<>();
            String id = rs.getString("id");
            String kind = rs.getString("kind");
            String introduction = rs.getString("introduction");
            String area = rs.getString("area");
            String address = rs.getString("address");
            Date beginTime = rs.getDate("begin_time");
            Date finishTime = rs.getDate("finish_time");
            String adminId = rs.getString("adminid");

            oneRecord.addElement(id);
            oneRecord.addElement(kind);
            oneRecord.addElement(introduction);
            oneRecord.addElement(area);
            oneRecord.addElement(address);
            oneRecord.addElement(beginTime);
            oneRecord.addElement(finishTime);
            oneRecord.addElement(adminId);


            data.addElement(oneRecord);
        }
        return data;
    }

    //添加功能
    @Override
    public boolean add(ZooparkEntity zooparkEntity) {

        StringBuilder sql=new StringBuilder();
        sql.append("INSERT INTO `zooparks` (`kind`,`introduction`,`area`,`address`,`begin_time`,`finish_time`,`adminid`) " +
                " VALUES (?,?,?,?,?,?,?)");

        Connection conn=null;
        PreparedStatement st=null;


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            st.setString(1,zooparkEntity.getKind());
            st.setString(2,zooparkEntity.getIntroduction());
            st.setString(3,zooparkEntity.getArea());
            st.setString(4,zooparkEntity.getAddress());
            st.setDate(5,zooparkEntity.getBeginTime());
            st.setDate(6,zooparkEntity.getFinishTime());
            st.setString(7, zooparkEntity.getAdminId());
            return st.executeUpdate()==1;


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,null);
        }
        return false;
    }
    //修改功能
    @Override
    public boolean update(ZooparkEntity zooparkEntity) {
        StringBuilder sql=new StringBuilder();
        sql.append("UPDATE `zooparks` SET `kind`=?,`introduction`=?,`area`=?,`address`=?, " +
                " `begin_time`=?,`finish_time`=?,`adminid`=? WHERE id=?");

        Connection conn=null;
        PreparedStatement st=null;


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            st.setString(1,zooparkEntity.getKind());
            st.setString(2,zooparkEntity.getIntroduction());
            st.setString(3,zooparkEntity.getArea());
            st.setString(4,zooparkEntity.getAddress());
            st.setDate(5,zooparkEntity.getBeginTime());
            st.setDate(6,zooparkEntity.getFinishTime());
            st.setString(7,zooparkEntity.getAdminId());
            st.setInt(8,zooparkEntity.getId());


            return st.executeUpdate()==1;


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,null);
        }

        return false;
    }

    @Override
    public boolean delete(ZooparkEntity zooparkEntity) {
        StringBuilder sql=new StringBuilder();
        sql.append("delete from `zooparks` where `id` =?");

        Connection conn=null;
        PreparedStatement st=null;


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());

            st.setInt(1,zooparkEntity.getId());


            return st.executeUpdate()==1;


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            com.JdbcUtils_DBCP.release(conn,st,null);
        }
        return false;
    }


}
